<?php
/**
 * Model 基类
 * @author solar
 * @version 1.0
 */
class BaseModel 
{
	public $table;
	public $id;
	public $db;
	
	function __construct($db,$table,$id='') {
		$this->db=$db;
		$this->table=$table;
		$this->id=$id;
	}
		
	public function get($id) {
		$sql="SELECT * FROM {$this->table} WHERE {$this->id}='{$id}' LIMIT 1";
		return $this->db->fetchRow($sql);
	}
	
	public function insert($row) {
		$sqlPart='';
		foreach($row as $key=>$value) $sqlPart.=",{$key}='{$value}'";
		$sql="INSERT INTO {$this->table} SET ".substr($sqlPart,1);
		$this->db->query($sql);
		return $this->db->insertID();
	}
	
	public function update($row,$id) {
		$sqlPart='';
		foreach($row as $key=>$value) $sqlPart.=",{$key}='{$value}'";
		$sql="UPDATE {$this->table} SET ".substr($sqlPart,1)." WHERE {$this->id}='{$id}' LIMIT 1";
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	public function remove($id) {
		$sql="DELETE FROM {$this->table} WHERE {$this->id}='{$id}' LIMIT 1";
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	public function getOne($id,$field) {
		$sql="SELECT {$field} FROM {$this->table} WHERE {$this->id}='{$id}' LIMIT 1";
		return $this->db->fetchOne($sql);
	}
	
	public function getOneByField($aField,$name) {
		foreach($aField as $key=>$value) $aCondition[]="{$key}='{$value}'";
		$sql="SELECT {$name} FROM {$this->table} WHERE ".implode(' AND ',$aCondition).' LIMIT 1';
		return $this->db->fetchOne($sql);
	}
	
	public function getByField($aField) {
		foreach($aField as $key=>$value) $aCondition[]="{$key}='{$value}'";
		$sql="SELECT * FROM {$this->table} WHERE ".implode(' AND ',$aCondition).' LIMIT 1';
		return $this->db->fetchRow($sql);
	}
	
	public function updateByField($row,$aField) {
		foreach($row as $key=>$value) $sqlPart.=",{$key}='{$value}'";
		foreach($aField as $key=>$value) $aCondition[]="{$key}='{$value}'";
		$sql="UPDATE {$this->table} SET ".substr($sqlPart,1)." WHERE ".implode(' AND ',$aCondition);
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	public function removeByField($aField) {
		foreach($aField as $key=>$value) $aCondition[]="{$key}='{$value}'";
		$sql="DELETE FROM {$this->table} WHERE ".implode(' AND ',$aCondition);
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	public function getAll($aEqualWhere=null,$aLikeWhere=null,$field='*') {
		$sql='SELECT '.$field.' FROM '.$this->table.' WHERE 1';
		if($aEqualWhere!==null) $sql.=' AND '.$this->makeEqualWhere($aEqualWhere);
		if($aLikeWhere!==null) $sql.=' AND '.$this->makeLikeWhere($aLikeWhere);
		return $this->db->fetchAll($sql);
	}
	
	public function getCount($aEqualWhere=null,$aLikeWhere=null) {
		$sql='SELECT COUNT(*) FROM '.$this->table.' WHERE 1';
		if($aEqualWhere!==null) $sql.=' AND '.$this->makeEqualWhere($aEqualWhere);
		if($aLikeWhere!==null) $sql.=' AND '.$this->makeLikeWhere($aLikeWhere);
		return $this->db->fetchOne($sql);
	}
	
	public function getList($offset,$limit,$orderBy=null,$sort=null,$aEqualWhere=null,$aLikeWhere=null,$field='*'){
		$sql='SELECT '.$field.' FROM '.$this->table.' WHERE 1';
		if($aEqualWhere!==null) $sql.=' AND '.$this->makeEqualWhere($aEqualWhere);
		if($aLikeWhere!==null) $sql.=' AND '.$this->makeLikeWhere($aLikeWhere);
		if($orderBy!==null && $sort!==null) $sql.=' ORDER BY '.$orderBy.' '.$sort;
		$sql.=' LIMIT '.$offset.','.$limit;
		return $this->db->fetchAll($sql);
	}
	
	protected function makeEqualWhere($aEqualWhere){
		foreach($aEqualWhere as $key=>$value){
			$aCondition[]=$key.'=\''.$value.'\'';
		}
		return implode(' AND ',$aCondition);
	}
	
	protected function makeLikeWhere($aLikeWhere){
		foreach($aLikeWhere as $key=>$value){
			$aCondition[]=$key.' LIKE \''.$value.'\'';
		}
		return implode(' AND ',$aCondition);
	}
			
}

?>